package UI;
import Ctr.*;
import Model.*;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JSeparator;

public class GUI {

	private JFrame frmKassesalg;
	private JTextField textField_1;
	private JTextField textField_2;
	private int vareNummer = 0;
	private double prisIAlt = 0;
	private String beskrivelse = "";
	private VareCtr vare = new VareCtr();
	private Vare varer = null;
	private double pris = 0;
	private VareContainer vc;
	private JTextField t = new JTextField();
	private JTextArea txtrRaekkeAntalVare = new JTextArea();
	private JLabel lblTotalPris = new JLabel("Total Pris: 0");
	private ArrayList<String> vareBeskrivelse = new ArrayList<String>();
	private ArrayList<Integer> antalVare = new ArrayList<Integer>();
	private ArrayList<Double> antalPris = new ArrayList<Double>();
	private JTextField txtIndtastRaekkeNummeret;
	private JLabel lblDato;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI window = new GUI();
					window.frmKassesalg.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GUI() {
		initialize();
		vc = VareContainer.getInstance();
		Vare v = new Vare(204.50, 535.50, 499, 1000, "Boremaskine");
		Vare v1 = new Vare(13, 55, 40, 1001, "Hammer");
		Vare v2 = new Vare(10, 34, 30, 1002, "Skruer");
		Vare v3 = new Vare(175, 500, 400, 1003, "stiksav");
		vc.addVare(v);
		vc.addVare(v1);
		vc.addVare(v2);
		vc.addVare(v3);
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() 
	{
		makeFrame();
		makeTextfields();
		makeButton();
		makeLabels();
		t.selectAll();
	}

	private void makeTextfields()
	{
		t.setText("Indtast Varenummer");
		t.setBounds(182, 161, 178, 22);
		frmKassesalg.getContentPane().add(t);
		t.setColumns(10);
		t.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) 
			{
				int key = e.getKeyCode();
			    if (key == KeyEvent.VK_ENTER) 
			    {
			    	enterButton();
			    }
			}
		});
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setBounds(182, 196, 178, 22);
		frmKassesalg.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(182, 231, 178, 22);
		frmKassesalg.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		textField_2.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) 
			{
				int key = e.getKeyCode();
			    if (key == KeyEvent.VK_ENTER) 
			    {
			    	antalVareButton();
			    }
			}
		});
	}
	
	private void makeButton()
	{
		lblTotalPris.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblTotalPris.setBounds(340, 592, 205, 25);
		frmKassesalg.getContentPane().add(lblTotalPris);
		txtrRaekkeAntalVare.setText("Raekke\tAntal\tVare\t\tpris");
		
		txtrRaekkeAntalVare.setEditable(false);
		txtrRaekkeAntalVare.setBounds(22, 337, 523, 242);
		txtrRaekkeAntalVare.setColumns(2);
		frmKassesalg.getContentPane().add(txtrRaekkeAntalVare);
		
		JButton btnFindNummer = new JButton("Find Vare Nummer");
		btnFindNummer.setMnemonic('F');
		btnFindNummer.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				enterButton();
			}});
		
		btnFindNummer.setBounds(394, 160, 151, 25);
		frmKassesalg.getContentPane().add(btnFindNummer);
		
		JButton btnNewButton = new JButton("Tilfoej Vare");
		btnNewButton.setMnemonic('T');
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				antalVareButton();
			}
		});
		btnNewButton.setBounds(394, 230, 151, 25);
		frmKassesalg.getContentPane().add(btnNewButton);
		
		JButton btnGennemfoer = new JButton("Gennemfoer");
		btnGennemfoer.setMnemonic('G');
		btnGennemfoer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("Vestbjerg Byggecenter\n");
				System.out.println(txtrRaekkeAntalVare.getText());
				System.out.println(lblTotalPris.getText());
				System.out.println("\nDato: " + Date.now());
				vareBeskrivelse.clear();
				antalVare.clear();
				antalPris.clear();
				lblTotalPris.setText("Total Pris: 0");
				textField_1.setText("");
				textField_2.setText("");
				txtrRaekkeAntalVare.setText("Raekke	Antal	Vare		pris\n");
				t.setText("Indtast Varenummer");
				t.grabFocus();
				t.selectAll();
				
				
			}
		});
		btnGennemfoer.setBounds(153, 594, 119, 25);
		frmKassesalg.getContentPane().add(btnGennemfoer);
	}
	private void makeLabels()
	{	
		JLabel lblNewLabel = new JLabel("Indtast Varenummer");
		lblNewLabel.setBounds(37, 164, 133, 16);
		frmKassesalg.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Vare Beskrivelse");
		lblNewLabel_1.setBounds(37, 199, 133, 16);
		frmKassesalg.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Antal vare");
		lblNewLabel_2.setBounds(37, 234, 97, 16);
		frmKassesalg.getContentPane().add(lblNewLabel_2);
		
		JLabel lblKvittering = new JLabel("Kvittering");
		lblKvittering.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblKvittering.setBounds(36, 311, 119, 25);
		frmKassesalg.getContentPane().add(lblKvittering);
		
		JButton btnExit = new JButton("Afbryd");
		btnExit.setMnemonic('A');
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnExit.setBounds(22, 594, 119, 25);
		frmKassesalg.getContentPane().add(btnExit);
		
		JLabel lblNewLabel_3 = new JLabel("");
		lblNewLabel_3.setIcon(new ImageIcon("C:\\Users\\Rasmus\\Dropbox\\gruppe awesome\\SLET IKKE !\\Logo.png"));
		lblNewLabel_3.setBounds(33, 13, 512, 132);
		frmKassesalg.getContentPane().add(lblNewLabel_3);
		
		JButton btnSletVare = new JButton("Slet Vare");
		btnSletVare.setMnemonic('S');
		btnSletVare.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtIndtastRaekkeNummeret.setEnabled(true);
				txtIndtastRaekkeNummeret.grabFocus();
				txtIndtastRaekkeNummeret.selectAll();
			}
		});
		btnSletVare.setBounds(394, 281, 151, 25);
		frmKassesalg.getContentPane().add(btnSletVare);
		
		txtIndtastRaekkeNummeret = new JTextField();
		txtIndtastRaekkeNummeret.setEnabled(false);
		txtIndtastRaekkeNummeret.setText("Indtast Raekke Nummeret");
		txtIndtastRaekkeNummeret.setBounds(182, 282, 178, 22);
		frmKassesalg.getContentPane().add(txtIndtastRaekkeNummeret);
		txtIndtastRaekkeNummeret.setColumns(10);
		txtIndtastRaekkeNummeret.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) 
			{
				int key = e.getKeyCode();
			    if (key == KeyEvent.VK_ENTER) 
			    {
			    	deleteVare(Integer.parseInt(txtIndtastRaekkeNummeret.getText()));
			    	txtIndtastRaekkeNummeret.setEnabled(false);
			    	txtIndtastRaekkeNummeret.setText("Indtast Raekke Nummeret");
			    }
			}
		});		
		
		JSeparator separator = new JSeparator();
		separator.setBounds(22, 266, 523, 2);
		frmKassesalg.getContentPane().add(separator);
		
		JLabel lblDato = new JLabel("Dato: " + Date.now());
		lblDato.setBounds(383, 316, 162, 16);
		frmKassesalg.getContentPane().add(lblDato);
	}
	private void makeFrame()
	{
		frmKassesalg = new JFrame();
		frmKassesalg.setResizable(false);
		frmKassesalg.getContentPane().setForeground(Color.WHITE);
		frmKassesalg.setTitle("Kassesalg");
		frmKassesalg.setBounds(100, 100, 589, 677);
		frmKassesalg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmKassesalg.getContentPane().setLayout(null);
	}
	private void enterButton()
	{
		vareNummer = Integer.parseInt(t.getText());
		
		if(vare.findVare(vareNummer) != null)
		{
			varer = vare.findVare(vareNummer);
			pris = varer.getSalgspris();
			beskrivelse = varer.getVareBeskrivelse() + " " + varer.getSalgspris() + " kr.";
			textField_1.setText(beskrivelse);
			textField_2.grabFocus();
			textField_2.selectAll();
			
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Vare Nummer blev ikke fundet");
		}
	}
	private void antalVareButton()
	{
		if(vare.findVare(vareNummer) != null)
		{
			if(textField_2.getText().equals(""))
			{
				textField_2.setText("1");
			}
			vareBeskrivelse.add(varer.getVareBeskrivelse());
			antalVare.add(Integer.parseInt(textField_2.getText()));
			prisIAlt = (pris*(Integer.parseInt(textField_2.getText())));
			antalPris.add(prisIAlt);
			formethod();
			forpris();
			t.grabFocus();
			t.selectAll();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Vare Nummer blev ikke fundet");
		}
	}
	private void formethod()
	{
		int ipris = 0;
		int iantal = 0;
		int rowAntal = 1;
		txtrRaekkeAntalVare.setText("Raekke	Antal	Vare		pris\n");
		for(String s : vareBeskrivelse)
		{
			txtrRaekkeAntalVare.append(rowAntal + ": 	" + antalVare.get(iantal).toString() + "	" + s + "		" + antalPris.get(ipris).toString() + " kr. \n");
			ipris++;
			iantal++;
			rowAntal++;
		}
	}
	private void forpris()
	{
		double price = 0;
		for(Double d : antalPris)
		{
			price += d;
		}
		lblTotalPris.setText("Total Pris: " + price + " kr.");
	}
	private void deleteVare(int nummer)
	{
		if(vareBeskrivelse.size() > (nummer-1))
		{
			vareBeskrivelse.remove(nummer-1);
			antalPris.remove(nummer-1);
			antalVare.remove(nummer-1);
			formethod();
			forpris();
			t.grabFocus();
			t.selectAll();
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Raekken blev ikke fundet");
		}

	}
}
